from contifyuser.models import Member
from contifyuser.forms import RegistrationFormConnectPeople
from django import forms
from django.conf import settings
from django import forms
import datetime
from StartGroup.models import Group, GroupPortrait
from django.contrib.auth.models import User
from utils import activity_module
from django.contrib.admin.widgets import AdminFileWidget
import re

attrs_dict = { 'class': 'required' }

class StartGroupForm(forms.Form):
    
    group_name = forms.CharField(max_length=25, required=True, label='GIVE YOUR GROUP A NAME', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'25'}))
    group_tagline = forms.CharField(max_length=50, required=True, label='GIVE IT A SOLID TAG LINE', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'50'}))
    group_desc = forms.CharField(max_length=200, required=False, label='DESCRIBE YOUR GROUP',
                                 widget=forms.Textarea(attrs={'size':'100'}))
    group_web_address = forms.CharField(max_length=10, required=True, label='WEB ADDRESS', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'10'}))
    group_country = forms.ChoiceField(required=False, choices=settings.COUNTRIES, label='COUNTRY',
                                widget=forms.Select())
    group_zipcode = forms.CharField(required=False, label='ZIPCODE',
                                widget=forms.TextInput(attrs={'size':'5'}))
    group_city = forms.CharField(max_length=25, required=True, label='GROUP CITY', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'25'}))
    group_univ = forms.CharField(max_length=100, required=False, label='College/University',
                                 widget=forms.TextInput(attrs={'size':'25'}))    
    group_created_date = datetime.date.today()
        
    def clean_group_web_address(self):
        # validate for alphanumberic web_address
        alphaNum = re.compile('\w+')
        parsedStr = alphaNum.match(self.cleaned_data['group_web_address']).group()
        
        if (len(self.cleaned_data['group_web_address']) != len(parsedStr)): 
            raise forms.ValidationError((u'Web Address can only contain Alpha Numeric Characters.'))
        
        return self.cleaned_data['group_web_address']
    
    def save(self, member_info):        
        member = member_info
        new_connectpeople_group = Group(member_user = member,
                                group_name=self.cleaned_data['group_name'],
                                group_tagline=self.cleaned_data['group_tagline'],
                                group_desc=self.cleaned_data['group_desc'],
                                group_web_address=self.cleaned_data['group_web_address'],
                                group_country=self.cleaned_data['group_country'],
                                group_zipcode=self.cleaned_data['group_zipcode'],
                                group_city=self.cleaned_data['group_city'],
                                group_univ=self.cleaned_data['group_univ'],                                
                                )
        
        new_connectpeople_group.save()
        # Save group to Graph node
        activity_module.setGroupNode(new_connectpeople_group.group_web_address, member_info.user.username, new_connectpeople_group.group_name)
        return new_connectpeople_group


class StartGroupFormNoMember(RegistrationFormConnectPeople):
    
    group_name = forms.CharField(max_length=25, required=True, label='GIVE YOUR GROUP A NAME', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'25'}))
    group_tagline = forms.CharField(max_length=50, required=True, label='GIVE IT A SOLID TAG LINE', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'50'}))
    group_desc = forms.CharField(max_length=200, required=False, label='DESCRIBE YOUR GROUP',
                                 widget=forms.Textarea(attrs={'size':'100'}))
    group_web_address = forms.CharField(max_length=10, required=True, label='WEB ADDRESS', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'10'}))
    group_country = forms.ChoiceField(required=False, choices=settings.COUNTRIES, label='COUNTRY',
                                widget=forms.Select())
    group_zipcode = forms.CharField(required=False, label='ZIPCODE',
                                widget=forms.TextInput(attrs={'size':'5'}))
    group_city = forms.CharField(max_length=25, required=True, label='GROUP CITY', error_messages={'required': 'This is a required field.'},
                                 widget=forms.TextInput(attrs={'size':'25'}))
    group_univ = forms.CharField(max_length=100, required=False, label='College/University',
                                 widget=forms.TextInput(attrs={'size':'25'}))      
    group_created_date = datetime.date.today()
    
    def clean_group_web_address(self):
        # validate for alphanumberic web_address
        alphaNum = re.compile('\w+')
        parsedStr = alphaNum.match(self.cleaned_data['group_web_address']).group()
        
        if (len(self.cleaned_data['group_web_address']) != len(parsedStr)): 
            raise forms.ValidationError((u'Web Address can only contain Alpha Numeric Characters.'))
        
        return self.cleaned_data['group_web_address']    
    
    def save(self):
        new_member = super(StartGroupFormNoMember, self).save()
        new_connectpeople_group = Group(member_user = new_member,
                                group_name=self.cleaned_data['group_name'],
                                group_tagline=self.cleaned_data['group_tagline'],
                                group_desc=self.cleaned_data['group_desc'],
                                group_web_address=self.cleaned_data['group_web_address'],
                                group_country=self.cleaned_data['group_country'],
                                group_zipcode=self.cleaned_data['group_zipcode'],
                                group_city=self.cleaned_data['group_city'],
                                group_univ=self.cleaned_data['group_univ'],
                                )
        new_connectpeople_group.save()
        # Save group to Graph node
        activity_module.setGroupNode(new_connectpeople_group.group_web_address, new_member.user.username, new_connectpeople_group.group_name)        
        return new_connectpeople_group
    
class GroupPortraitForm(forms.Form):
    
    image = forms.ImageField(label='Photo', required=False, widget = AdminFileWidget())
        
    def save(self, group_web_addr):
        group_info = Group.objects.get(group_web_address=group_web_addr)
        new_group_portrait = GroupPortrait(group = group_info,
                                image=self.cleaned_data['image'],
                                )
        new_group_portrait.save()
        return new_group_portrait